<?php

namespace Home\Util;

	class OrderUtil{
		private $Orders = array();
		private $order = array(
				'UNID' => '',  //编号
				'OPENID' => '', //公众用户openid
				'AIRPORT' => '', //机场
				'PDATE' => '', //接机日期
				'PTIME' => '', //接机时间
				'MODELS' => '', //车型
				'NUMBER' => '', //座数
				'DESTINATION' => '', //目的地
				'WXNUM' => '', //微信号
				'FLIGHT' => '', //航班号
				'PHONE' => '', //联系方式
				'COST' => '', //报价
				'SOURCE' => '', //订单来源的平台( 自己平台 其他平台来源可能已经支付  )
				'TYPE' => '', //订单的类型 0 用户发起  1 车队发起  2 车队发起调配
				'STATE' => ''  //订单状态  0 未配对车队 1 车队已接单 2 司机已接待到用户.....
			);
		static private $orderService;
		/**
		 * [__construct]
		 * @Author   :Zengyi
		 * @DateTime :2016-12-02T09:42:51+0800
		 * @Detail   :封装的数据类
		 */
		// public function __construct($UNID,$OPENID,$AIRPORT,$PDATE,$PTIME,$MODELS,$NUMBER,$DESTINATION,$WXNUM,$FLIGHT,$PHONE,$COST,$SOURCE,$TYPE,$STATE){
		// 	self::$orderService = Service('Order','order','Home');
		// 		$this->order['UNID'] = $UNID;
		// 		$this->order['OPENID'] = $OPENID;
		// 		$this->order['AIRPORT'] = $AIRPORT;
		// 		$this->order['PDATE'] = $PDATE;
		// 		$this->order['PTIME'] = $PTIME;
		// 		$this->order['MODELS'] = $MODELS;
		// 		$this->order['NUMBER'] = $NUMBER;
		// 		$this->order['DESTINATION'] = $DESTINATION;
		// 		$this->order['WXNUM'] = $WXNUM;
		// 		$this->order['FLIGHT'] = $FLIGHT;
		// 		$this->order['PHONE'] = $PHONE;
		// 		$this->order['COST'] = $COST;
		// 		$this->order['SOURCE'] = $SOURCE;
		// 		$this->order['TYPE'] = $TYPE;
		// 		$this->order['STATE'] = $STATE;
		// }
		public function __construct($data){
			self::$orderService = Service('Order','order','Home');
			$this->order = $data;
		}

		/**
		 * [addOrder]
		 * @Author   :Zengyi
		 * @DateTime :2016-12-02T09:42:34+0800
		 * @Detail   :新增订单
		 */
		public function addOrder($data){
			return self::$orderService->insert($data);
		}

		/**
		 * [updateFields]
		 * @Author   :Zengyi
		 * @DateTime :2016-12-01T10:45:14+0800
		 * @Detail   :修改订单一个字段
		 */
		public function updateFields($unid,$fields = NULL,$value = NULL){
			$data['UNID'] = $unid;
			if(!is_null($fields)&&!is_null($value)) $data[$fields] = $value;
			return self::$orderService->update($data);
		}

		/**
		 * [updateOrder]
		 * @Author   :Zengyi
		 * @DateTime :2016-12-01T10:45:14+0800
		 * @Detail   :修改订单
		 */
		public function updateOrder($data){
			return self::$orderService->update($data);
		}

		/**
		 * [deleteOrder]
		 * @Author   :Zengyi
		 * @DateTime :2016-12-02T10:46:03+0800
		 * @Detail   :订单删除标记
		 */
		public function deleteOrder($unid){
			$data['UNID'] = $unid;
			$data['DEL'] = 1;

			return self::$orderService->update($data);
		}

		/**
		 * [removeOrder]
		 * @Author   :Zengyi
		 * @DateTime :2016-12-01T10:48:36+0800
		 * @Detail   :移除相应的order
		 */
		public function removeOrder($condition){
			return self::$orderService->remove($condition);
		}


		/**
		 * [findOrder]
		 * @Author   :Zengyi
		 * @DateTime :2016-12-01T16:54:58+0800
		 * @Detail   :根据unid查找订单(单个)
		 */
		public function findOrderByUnid($unid){
			return self::$orderService->find($unid);
		}

		/**
		 * [queryOrders]
		 * @Author   :Zengyi
		 * @DateTime :2016-12-01T10:55:20+0800
		 * @Detail   :根据条件查找订单(列表)
		 */
		public function queryOrders($condition,$data,$limit){
			return self::$orderService->queryFields($condition,$data,$limit);
		}

		/**
		 * [queryOrdersPage]
		 * @Author   :ZengYi
		 * @DateTime :2016-12-08T14:44:05+0800
		 * @Detail   :根据条件查找订单总数
		 * @param    [type]                    $condition [description]
		 * @param    [type]                    $data      [description]
		 * @return   [type]                               [description]
		 */
		public function queryOrdersCount($condition){
			return self::$orderService->queryCount($condition);
		}

		/**
		 * [queryAllocateLimit]
		 * @Author   :Zengyi
		 * @DateTime :2016-12-01T10:55:20+0800
		 * @Detail   :根据条件查找订单(列表)type司机0,客服1
		 */
		public function queryOrderLimit($condition,$limit,$uid,$type){
			return self::$orderService->queryOrderLimit($condition,$limit,$uid,$type);
		}

		/**
		 * [queryAllocateLimitCount]
		 * @Author   :Zengyi
		 * @DateTime :2016-12-01T10:55:20+0800
		 * @Detail   :根据条件查找订单(列表)type司机0,客服1   单总数
		 */
		public function queryOrderLimitCount($condition,$uid,$type){
			return self::$orderService->queryOrderLimitCount($condition,$uid,$type);
		}

		/**
		 * [queryAllocateLimit]
		 * @Author   :Zengyi
		 * @DateTime :2016-12-01T10:55:20+0800
		 * @Detail   :根据条件查找订单(列表)调配
		 */
		public function queryAllocateLimit($condition,$limit,$team){
			return self::$orderService->queryAllocateLimit($condition,$limit,$team);
		}

		/**
		 * 设置默认下单车队
		 * @Author   :ZengYi
		 * @DateTime :2017-02-09T18:16:21+0800
		 * @Detail   :
		 */
		public function setSetTidSession($setTid,$defaultTid){
			$ts=Service('Team','team');
	        if ($setTid) {
	            $TID=$setTid;
	            $teamData=$ts->find("UNID='$TID'");
	            if(!empty($teamData)){
	            	if($teamData['LOCK']==1){  //若get获得的车队被管理员锁定，则下单的车队改为默认车队
		                $TID=$defaultTid;
		                $teamData=$ts->find("UNID='$TID'");
		            }
	            }else{
	            	$TID=$defaultTid;
		            $teamData=$ts->find("UNID='$TID'");
	            }
	        } else {
	            $TID=$defaultTid;
	            $teamData=$ts->find("UNID='$TID'");
	        }
	        return $teamData;
		}
	}
